package com.beaven.imclient.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import com.beaven.imclient.R;
import com.beaven.imclient.base.BaseHideActivity;
import com.beaven.imclient.fragment.LoginFragment;
import com.beaven.imclient.fragment.RegisterFragment;
import com.beaven.imclient.utils.ActivityUtil;

public class LoginActivity extends BaseHideActivity
    implements LoginFragment.OnLoginInteractionListener {

  private Toolbar toolbar;
  private Fragment currentFragment;
  private TextView text_content_center;
  private String contentText;

  public static void start(Context context) {
    context.startActivity(new Intent(context, LoginActivity.class));
  }

  @Override public int getLayoutId() {
    return R.layout.activity_login;
  }

  @Override protected void initWidget() {
    super.initWidget();
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    text_content_center = (TextView) findViewById(R.id.text_content_center);
  }

  @Override protected void initData() {
    super.initData();
    toLogin();
  }

  @Override public void toggleFragment() {
    if (currentFragment instanceof LoginFragment) {
      toRegister();
    } else {
      toLogin();
    }
  }

  // 跳转注册界面
  private void toRegister() {
    currentFragment = RegisterFragment.newInstance();
    toolbar.setNavigationIcon(R.drawable.image_back_left);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        toLogin();
      }
    });
    contentText = getString(R.string.register);
    text_content_center.setText(contentText);
    ActivityUtil.replaceFragmentToActivity(getSupportFragmentManager(), currentFragment,
        R.id.layout_login_frame);
  }

  // 跳转登录界面
  private void toLogin() {
    currentFragment = LoginFragment.newInstance();
    contentText = getString(R.string.login);
    toolbar.setNavigationIcon(null);
    text_content_center.setText(contentText);
    ActivityUtil.replaceFragmentToActivity(getSupportFragmentManager(), currentFragment,
        R.id.layout_login_frame);
  }

  @Override public void onBackPressed() {
    if (currentFragment instanceof LoginFragment) {
      super.onBackPressed();
      finish();
    } else {
      toLogin();
    }
  }
}
